package com.yunxi.service.dubboService.impl.dealer;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.BusinessVO;
import com.yunxi.dealer.service.BusinessDubboService;
import com.yunxi.model.dealer.Business;
import com.yunxi.service.localService.dealer.BusinessService;
import com.yunxi.util.RedisCacheService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 渠道商业务类型信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-21
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class BusinessDubboServiceImpl implements BusinessDubboService {

   @Autowired
   @Qualifier(value = "businessServiceImpl")
   BusinessService businessService;

   @Autowired
   RedisCacheService redisCacheService;

   @Override
   public BusinessVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      Business business = businessService.getById(id);
      BusinessVO businessVO = new BusinessVO();
      BeanUtils.copyProperties(business, businessVO);
      return businessVO;
   }

   @Override
   public void saveOrUpdate(BusinessVO businessVO, LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      if(StringUtils.isEmpty(businessVO.getBusinessId())){
         businessVO.setCreateBy(loginInfo.getRealName());
      }
      businessVO.setUpdateBy(loginInfo.getRealName());
      Business business = new Business();
      BeanUtils.copyProperties(businessVO, business);
      businessService.saveOrUpdate(business);
      redisCacheService.deleteKeys("business");
   }

   @Override
   public void removeById(String id, LoginInfo loginInfo) {
      Business business = businessService.getById(id);
      business.setUpdateBy(loginInfo.getRealName());
      businessService.removeById(id);
      redisCacheService.deleteKeys("business");
   }

   @Override
   public PageInfo<BusinessVO> findPageByVO(BusinessVO businessVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<BusinessVO> pageInfo = businessService.pageByVo(businessVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   @Cacheable(value="business",key = "'business_list'")
   public List listByVo(BusinessVO v) {
      return businessService.listByVo(v);
   }

}
